extends ConfirmationDialog

#region const
## 不保存常量
const UNSAVE: String = "不保存"
#endregion const

#region signal
## 不保存时引发的事件
signal unsaved()
#endregion signal

#region variables
## 确认对话框的原生标签，将会被隐藏
var _label: Label

## 新增的纹理按钮，用于显示图标
var _textureButton: TextureButton

## 新增的富文本标签，用于显示提示内容
var _richTextLabel: RichTextLabel

## 新增的不保存按钮
var _buttonUnsave: Button

## 对话框中显示的图标纹理
@export var IconTexture: Texture2D:
	get:
		return IconTexture
	set(value):
		IconTexture = value
		if _textureButton:
			_textureButton.texture_normal = IconTexture

## 对话框中显示的富文本，可以使用BBCode
@export_multiline var RichText: String = "[b]是否在{0}前保存对PAK文件【{1}】的修改？[/b]\r\n若不保存，您的更改将会丢失。":
	get:
		return RichText
	set(value):
		RichText = value
		if _richTextLabel:
			_richTextLabel.text = value
			
## 不保存按钮的文本
@export var UnsaveText: String = UNSAVE
#endregion variables

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	_label = get_label()
	_label.visible = false
	_textureButton = $HBoxContainer/TextureButton as TextureButton
	_textureButton.texture_normal = IconTexture
	_richTextLabel = $HBoxContainer/RichTextLabel as RichTextLabel
	_richTextLabel.text = RichText
	_buttonUnsave = add_button(UnsaveText, true, UNSAVE)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass

## 添加按钮的事件处理：如果连接了unsaved信号，触发它。
func _on_custom_action(action: StringName) -> void:
	if (action == UNSAVE and not unsaved.is_null() and 
		not unsaved.get_connections().is_empty()):
		unsaved.emit()
	hide()
